package com.cy.demo1.service.impl;

import com.cy.demo1.entity.Data;
import com.cy.demo1.mapper.DataMapper;
import com.cy.demo1.mapper.UserMapper;
import com.cy.demo1.service.IDataService;
import com.cy.demo1.service.ex.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DataServiceImpl implements IDataService {
    @Resource
    private DataMapper dataMapper;

    @Override
    public Data[] showData() {
        Integer number = dataMapper.calculateProject();
        Data result[] = new Data[number];
        Integer max = dataMapper.getMax();
        int j = 0;
        for (int i = 1; i < max + 1; i++){
            if (dataMapper.listProject(i) != null){
                result[j] = dataMapper.listProject(i);
                j++;
            }
        }
        return result;
    }

    @Override
    public void uploadData(Data data) {
        String projectName = data.getProjectName();
        //判断项目名是否被占用
        Data result = dataMapper.findByProjectName(projectName);
        if(result != null) {
            throw new ProjectNameDuplicatedException("项目名已存在");
        }

        Integer rows = dataMapper.upload(data);
        if (rows != 1) {
            throw new InsertException("项目上传过程中产生未知异常");
        }

    }

    @Override
    public Integer countProject() {
        Integer number = dataMapper.calculateProject();
        return number;
    }
}
